package com.jandar.ds.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jandar.ds.mapper.DbScriptMapper;
import com.jandar.ds.pojo.DbScript;
import com.jandar.ds.pojo.DbScriptExample;
import com.jandar.ds.pojo.ListQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author xiongxx
 * @date 2022/6/30 15:57
 * @description
 */
@Service
public class DbScriptService {
    @Autowired
    private DbScriptMapper dbScriptMapper;


    public PageInfo<DbScript> list(ListQuery<DbScript> dbScriptListQuery) {
        PageHelper.startPage(dbScriptListQuery.getPage(), dbScriptListQuery.getRows());
        DbScript dbScript = dbScriptListQuery.getEntity();
        DbScriptExample example = new DbScriptExample();
        DbScriptExample.Criteria criteria = example.createCriteria();
        if (dbScript.getName() != null) {
            criteria.andNameLike("%" + dbScript.getName() + "%");
        }
        if (dbScript.getUserName() != null) {
            criteria.andUserNameEqualTo(dbScript.getUserName());
        }
        List<DbScript> list = dbScriptMapper.selectByExample(example);
        return PageInfo.of(list);
    }

    public DbScript get(Integer id) {
        return dbScriptMapper.selectByPrimaryKey(id);
    }

    public void insert(DbScript dbScript) {
        dbScript.setCreateTime(LocalDateTime.now());
        dbScriptMapper.insert(dbScript);
    }

    public void update(DbScript dbScript) {
        dbScriptMapper.updateByPrimaryKeySelective(dbScript);
    }

    public void delete(Integer id) {
        dbScriptMapper.deleteByPrimaryKey(id);
    }
}
